﻿/// <reference path="../Extension.js" />
/// <reference path="../Default.js" />

$(document).ready(function () {
    $('.Accordion').accordion({
        autoHeight: false,
        navigation: true,
        collapsible: true,
        active: false
    });

    $("#tCnaes").jqGrid({
        datatype: 'json',
        url: '/Lojas/Cnaes',
        mtype: 'GET',
        colNames: ['', 'Id', 'Cnae', 'Descrição'],
        colModel: [
            { name: "actions", formatter: "actions", editable: false, sortable: false, resizable: false, fixed: true, align: 'center', width: 26, "formatoptions": { keys: false, editbutton: false, delbutton: true} },
            { name: 'ID_CNLJ', index: 'ID_CNLJ', width: 80, sortable: false, align: 'right', search: false, hidden: true },
            { name: 'COD_CNAE', index: 'COD_CNAE', width: 100, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'DESC_CNAE', index: 'DESC_CNAE', width: 600, align: 'left', editable: true, searchoptions: { sopt: ['cn', 'eq']} }
        ],
        gridComplete: function () { loadInputs(); },
        pager: '#pCnaes',
        height: 'auto',
        width: 500,
        rowNum: 5,
        rowList: [5, 10, 15, 20],
        sortname: 'COD_CNAE',
        sortorder: "asc",
        autowidth: false,
        viewrecords: true,
        viewsortcols: true,
        gridview: true,
        multiselect: false,
        editurl: '/Lojas/CnaeDelete',
        postData: {
            COD_LOJA: function () { return $('#COD_LOJA').val(); }
        }
    }).navGrid("#pCnaes", {
        edit: false, add: false, del: false, search: false, refresh: true
    }).navButtonAdd("#pCnaes", {
        caption: '',
        buttonicon: 'ui-icon-circle-plus',
        onClickButton: function () {
            loadCnaes();
        },
        title: "adicionar cnae",
        position: "last"
    });

    $("#tListas").jqGrid({
        datatype: 'json',
        url: '/Lojas/Listas',
        mtype: 'GET',
        colNames: ['', 'Id', 'Codigo', 'Descrição'],
        colModel: [
            { name: "actions", formatter: "actions", editable: false, sortable: false, resizable: false, fixed: true, align: 'center', width: 26, "formatoptions": { keys: false, editbutton: false, delbutton: true} },
            { name: 'ID_LSLJ', index: 'ID_LSLJ', width: 80, sortable: false, align: 'right', search: false, hidden: true },
            { name: 'COD_LISTA', index: 'COD_LISTA', width: 80, sortable: false, align: 'right', search: false, hidden: true },
            { name: 'DESC_LISTA', index: 'DESC_LISTA', width: 600, align: 'left', searchoptions: { sopt: ['cn', 'eq']} }
        ],
        altRows: false,
        pager: '#pListas',
        height: 'auto',
        width: 500,
        rowNum: 5,
        rowList: [5, 10, 15, 20],
        sortname: 'DESC_LISTA',
        sortorder: "asc",
        autowidth: false,
        viewrecords: true,
        viewsortcols: true,
        gridview: true,
        multiselect: false,
        editurl: '/Lojas/ListaDelete',
        postData: {
            COD_LOJA: function () { return $('#COD_LOJA').val(); }
        }
    }).navGrid("#pListas", {
        edit: false, add: false, del: false, search: false, refresh: true
    }).navButtonAdd("#pListas", {
        caption: '',
        buttonicon: 'ui-icon-circle-plus',
        onClickButton: function () {
            loadListas();
        },
        title: "adicionar cnae",
        position: "last"
    });
});

function loadCnaes() {
    progressDialog(true);
    var lookup = $('<div id="__cnaes" style="display: none;" />');
    lookup.appendTo('body');

    $(lookup).load('/Default/Lookup', { ID_FNPR: 8610 }, function (data) {
        $("#lookupTable").jqGrid({
            url: '/Cnaes/List',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Id', 'Código', 'Descrição'],
            colModel: [
                { name: 'ID_CNAE', index: 'ID_CNAE', width: 80, align: 'right', hidden: true },
                { name: 'COD_CNAE', index: 'COD_CNAE', width: 80, align: 'right' },
                { name: 'DESC_CNAE', index: 'DESC_CNAE', width: 500 }
            ],
            pager: '#lookupPager',
            width: '100%',
            height: '100%',
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: 'COD_CNAE',
            sortorder: 'asc',
            viewrecords: true,
            gridComplete: function () {
                $(lookup).dialog({
                    width: 'auto',
                    height: 'auto',
                    title: 'Editar Cnaes da Loja',
                    modal: true,
                    position: 'center',
                    buttons: {
                        'Cancelar': function () {
                            $(lookup).dialog('destroy').remove();
                        }
                    },
                    open: function () {
                        progressDialog(false);
                        lookup.removeAttr("style");
                    }
                });
            },
            ondblClickRow: function (id) {
                progressDialog(true);
                $.post('/Lojas/CnaeUpdate', { ID_CNAE: id, COD_LOJA: $('#COD_LOJA').val() }, function (data) {
                    progressDialog(false);
                    if (data.result == true) {
                        $(lookup).dialog('destroy').remove();
                        $('#tCnaes').trigger('reloadGrid');
                    }
                    else
                        listErros(data.erros);
                });
            }
        });
        $("#lookupTable").jqGrid('filterToolbar', { stringResult: false, searchOnEnter: false });
        $("#lookupTable").jqGrid('navGrid', '#lookupPager', { del: false, add: false, edit: false, search: false });
    });
}

function loadListas() {
    progressDialog(true);
    var lookup = $('<div id="__listas" style="display: none;" />');
    lookup.appendTo('body');

    $(lookup).load('/Default/Lookup', { ID_FNPR: 8610 }, function (data) {
        $("#lookupTable").jqGrid({
            url: '/Listas/List',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Código', 'Descrição','Inicio','Termino'],
            colModel: [
                { name: 'COD_LISTA', index: 'COD_LISTA', width: 80, align: 'right', hidden: true },
                { name: 'DESC_LISTA', index: 'DESC_LISTA', width: 500 },
                { name: 'VALIDADE_INICIO_LISTA', index: 'VALIDADE_INICIO_LISTA', width: 80, align: 'right', hidden: true },
                { name: 'VALIDADE_TERMINO_LISTA', index: 'VALIDADE_TERMINO_LISTA', width: 80, align: 'right', hidden: true }
            ],
            pager: '#lookupPager',
            width: '100%',
            height: '100%',
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: 'DESC_LISTA',
            sortorder: 'asc',
            viewrecords: true,
            gridComplete: function () {
                $(lookup).dialog({
                    width: 'auto',
                    height: 'auto',
                    title: 'Editar Listas de Preço da Loja',
                    modal: true,
                    position: 'center',
                    buttons: {
                        'Cancelar': function () {
                            $(lookup).dialog('destroy').remove();
                        }
                    },
                    open: function () {
                        progressDialog(false);
                        lookup.removeAttr("style");
                    }
                });
            },
            ondblClickRow: function (id) {
                progressDialog(true);
                $.post('/Lojas/ListaUpdate', { COD_LISTA: id, COD_LOJA: $('#COD_LOJA').val() }, function (data) {
                    progressDialog(false);
                    if (data.result == true) {
                        $(lookup).dialog('destroy').remove();
                        $('#tListas').trigger('reloadGrid');
                    }
                    else
                        listErros(data.erros);
                });
            }
        });
        $("#lookupTable").jqGrid('filterToolbar', { stringResult: false, searchOnEnter: false });
        $("#lookupTable").jqGrid('navGrid', '#lookupPager', { del: false, add: false, edit: false, search: false });
    });
}